home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8144 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  61 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.sprintlink.net!eskimo!scs
  3. From: scs@eskimo.com (Steve Summit)
  4. Subject: Re: What is &Variable (declared as: char Variable[10])?
  5. X-Nntp-Posting-Host: eskimo.com
  6. Message-ID: <DnLx2r.323@eskimo.com>
  7. Sender: news@eskimo.com (News User Id)
  8. Organization: schmorganization
  9. References: <4gqpa1$3h9@alcor.usc.edu> <4gsdno$1bg@umbc9.umbc.edu> <4gvlnlINNppj@anvil.ugrad.cs.ubc.ca>
  10. Date: Fri, 1 Mar 1996 21:01:38 GMT
  11.  
  12. In article <4gvlnlINNppj@anvil.ugrad.cs.ubc.ca>, c2a192@ugrad.cs.ubc.ca
  13. (Kazimir Kylheku) writes:
  14. > In article <4gsdno$1bg@umbc9.umbc.edu>,
  15. > Jonas J. Schlein <schlein@umbc.edu> wrote:
  16. >> I'm having trouble understanding why it matters? You almost never use the
  17. >> address of an array directly unless doing something tricky with pointers
  18. >> or with particular dimensions of a multiple dimensional array.
  19. > Not necessarily. The address of an array is very useful in
  20. > abstracting away the representation of a variable that happens
  21. > to be represented as an array...  The client can declare a
  22. > variable of [abstract] type... without caring whether it is a
  23. > struct, double, long, array or union.
  24.  
  25. What you say about abstract types is, in general, true, but it's
  26. probably not a good idea to instantiate an abstract type with an
  27. underlying array type, because arrays are second-class citizens
  28. in C.
  29.  
  30. A client programmer might reasonably assume that the sequence
  31.  
  32.     abstract_t x;
  33.     f(x);
  34.  
  35. could not modify x.  In C, this assumption is of course not valid
  36. if abstract_t happens to have array type.
  37.  
  38. As it happens, standard C does contain one abstract type which
  39. is, underneath, an array: jmp_buf, from <setjmp.h>.  Because
  40. setjmp() is traditionally called as
  41.  
  42.     jmp_buf env;
  43.     setjmp(env);
  44.  
  45. and because setjmp() is in fact specified as accepting a
  46. parameter of type jmp_buf, but since setjmp must modify its
  47. argument, we are left with the peculiar requirement that jmp_buf,
  48. though seemingly abstract, must be an array type.
  49.  
  50. If, one day, you find yourself wanting to implement an abstract
  51. type with an array, it's a good idea to wrap the array in a
  52. structure, so that it will be a first-class type.  Otherwise, you
  53. may run into surprises (when your callers find their variables
  54. being unexpectedly modified by calls to your routines) or you may
  55. accidentally lock yourself into maintaining the allegedly
  56. abstract type as an array type (as happened with jmp_buf).
  57.  
  58.                     Steve Summit
  59.                     scs@eskimo.com
  60.